package cn.match;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by liang on 2016/8/15.
 */
public class Imooc {
    private static String[] course = {"php","java","python","C","C+puls+puls","Go","C%23"};
    private static String imoocUrl = "http://www.imooc.com/course/list?c=%s&page=";
    public static void main(String[] args) {
        for (String c : course) {
            String imgUrl = String.format(imoocUrl, c);
            int p = 1;
            while (true) {
                ArrayList<String> imgList = getImageSrc(getBody(imgUrl+Integer.toString(p)));
                System.out.println(imgUrl+Integer.toString(p));
                if (imgList.size() == 0) break ;
                for (String item : imgList) {
                    try {
                        download(item, Long.toString(new Date().getTime()) + ".jpg", "D:\\img");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                ++p;
            }
        }
        System.out.println("ok");
    }
    /**
     * 设置url
     * @param imoocUrl
     */
    public static void setImoocUrl(String imoocUrl) {
        Imooc.imoocUrl = imoocUrl;
    }

    /**
     * 获取页面源代码
     * @return String
     */
    private static String getBody(String imgUrl){
        try {
            URL url = new URL(imgUrl);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            String line = "";
            while(reader.readLine()!=null){
                line=line+reader.readLine()+"\r\n";
            }
            return line;
        }catch (MalformedURLException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取图片的url
     * @param  htmlCode 网页内容
     * @return ArrayList<String>
     */
    private static ArrayList<String> getImageSrc(String htmlCode) {
        Pattern p = Pattern.compile("<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>");
        Matcher m = p.matcher(htmlCode);
        ArrayList<String> list = new ArrayList<String>();
        while(m.find()){
//            System.out.println(m.group());
//            System.out.println(m.group(1));
            if(m.group(1).indexOf("http://")>=0) {
                list.add(m.group(1));
            }
        }
        return list;
    }

    /**
     * 下载图片
     * @param urlString
     * @param filename
     * @param savePath
     * @throws Exception
     */
    private static void download(String urlString,String filename,String savePath) throws Exception
    {
        URL url = new URL(urlString);
        URLConnection con = url.openConnection();
        con.setConnectTimeout(5*10000);
        InputStream is = con.getInputStream();
        byte[] bs = new byte[1024];
        int len;
        File sf = new File(savePath);
        if(!sf.exists())    sf.mkdirs();
        OutputStream os = new FileOutputStream(sf.getPath()+"\\"+filename);
        while ((len = is.read(bs))!=-1){
            os.write(bs,0,len);
        }
        os.close();
        is.close();
    }
}
